package com.gxuwz.weather.citymanager;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.gxuwz.weather.R;
import com.gxuwz.weather.adapter.CitySearchAdapter;
import com.gxuwz.weather.db.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CityAddActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private ImageView search;
    private EditText searchEt;
    private ListView searchLv;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_add);

        back = findViewById(R.id.add_iv_back);
        back.setOnClickListener(this);

        search = findViewById(R.id.search_iv_submit);
        search.setOnClickListener(this);

        searchEt = findViewById(R.id.search_et);
        searchLv = findViewById(R.id.search_lv);

        dbHelper = new DBHelper(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.add_iv_back:
                intent.setClass(this, CityManagerActivity.class);
                startActivity(intent);
                break;
            case R.id.search_iv_submit:
                performSearch();
                break;
        }
    }

    @SuppressLint("Range")
    private void performSearch() {
        String keyword = searchEt.getText().toString().trim();
        if (keyword.isEmpty()) {
            return;
        }

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("area", new String[]{"areaCn", "cityCn", "areaId", "areaCode"},
                "areaCn LIKE ? OR cityCn LIKE ? OR areaId LIKE ?",
                new String[]{"%" + keyword + "%", "%" + keyword + "%", "%" + keyword + "%"},
                null, null, null);

        List<Map<String, String>> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            Map<String, String> cityInfo = new HashMap<>();
            cityInfo.put("areaCn", cursor.getString(cursor.getColumnIndex("areaCn")));
            cityInfo.put("cityCn", cursor.getString(cursor.getColumnIndex("cityCn")));
            cityInfo.put("areaId", cursor.getString(cursor.getColumnIndex("areaId")));
            cityInfo.put("areaCode", cursor.getString(cursor.getColumnIndex("areaCode")));
            resultList.add(cityInfo);
        }
        cursor.close();

        CitySearchAdapter adapter = new CitySearchAdapter(this, resultList);
        searchLv.setAdapter(adapter);

        searchLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, String> selectedCity = resultList.get(position);
                String areaName = selectedCity.get("areaCn");
                String cityCn = selectedCity.get("cityCn");
                String areaId = selectedCity.get("areaId");
                String areaCode = selectedCity.get("areaCode");

                // 添加到 myarea 表
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("areaId", areaId);
                values.put("areaCn", areaName);
                values.put("cityEn", "");
                values.put("cityCn", cityCn);
                values.put("provEn", "");
                values.put("areaEn", "");
                values.put("provCn", "");
                values.put("areaCode", areaCode);
                db.insert("myarea", null, values);

                // 跳转到 CityManagerActivity
                Intent intent = new Intent(CityAddActivity.this, CityManagerActivity.class);
                startActivity(intent);
            }
        });
    }
}
